var mongoose = require('mongoose')

var db = require('../server/db')

var modelName = 'visit'

var schema = new mongoose.Schema({
  ip: String,
  count: {type: Number, default: 1}
}, {timestamp: true})


schema.statics.create = function (ip, cb) {
  return new this({ip: ip}).save(cb)
}
schema.statics.getAll = function (cb) {
  return this.find({}).sort({order: -1}).exec(cb)
}
schema.statics.getByIp = function (ip, cb) {
  return this.findOne({ip: ip}).exec(cb)
}
schema.statics.increaseCount = function (ip, cb) {
  return this.findOneAndUpdate({ip: ip}, {$inc: {count: 1}}, {upsert: true, new: true}).exec(cb)
}

var model = db.model(modelName, schema)


module.exports = model